package view;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.LayoutManager;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;


public class PanelWithBorder extends JPanel {

	private static final long serialVersionUID = 7091747895470487068L;

	private JPanel pnlContainer = null;

	private JLabel lblBottomRight;
	private JLabel lblTopCenter;
	private JLabel lblTitle;
	private JLabel lblTopLeft;
	private JLabel lblTopRight;
	private JPanel pnlMiddleCenter;
	private JLabel lblMiddleLeft;
	private JLabel lblMiddleRight;
	private JLabel lblBottomCenter;
	private JLabel pnlBottomLeft;

	private JPanel pnlTop = null;

	private JPanel pnlMiddle = null;

	private JPanel pnlBottom = null;

	/**
	 * This method initializes pnlContainer	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlContainer() {
		if (pnlContainer == null) {
			pnlContainer = new JPanel();
			BoxLayout pnlContainerLayout = new BoxLayout(pnlContainer, javax.swing.BoxLayout.Y_AXIS);
			pnlContainer.setLayout(pnlContainerLayout);
//			pnlContainer.setFocusTraversalKeysEnabled(false);
			int width = (super.getWidth() > 0) ? super.getWidth() : (int) super.getPreferredSize().getWidth();
			int height = (super.getHeight() > 0) ? super.getHeight() : (int) super.getPreferredSize().getHeight();
			pnlContainer.setBounds(0, 0, width, height);
//			pnlContainer.setBorder(BorderFactory.createLineBorder(Color.GREEN));
			pnlContainer.add(getPnlTop());
			pnlContainer.add(getPnlMiddle());
			pnlContainer.add(getPnlBottom());
			
			stretchBorderImages();
		}
		return pnlContainer;
	}

	@Override
	public Component add(Component comp) {
		return this.getPnlMiddleCenter().add(comp);
	}
	@Override
	public Component add(Component comp, int index) {
		return this.getPnlMiddleCenter().add(comp, index);
	}

	@Override
	public void add(Component comp, Object constraints) {
		this.getPnlMiddleCenter().add(comp, constraints);
	}
	@Override
	public void add(Component comp, Object constraints, int index) {
		this.getPnlMiddleCenter().add(comp, constraints, index);
	}
	@Override
	public void setLayout(LayoutManager mgr) {
		this.getPnlMiddleCenter().setLayout(mgr);
	}
	
	@Override
	public void setSize(Dimension d) {
		super.setSize(d);
		getPnlContainer().setSize(d);
		
		stretchBorderImages();
	}
	
	@Override
	public void setSize(int width, int height) {
		super.setSize(width, height);
		getPnlContainer().setSize(width, height);
		stretchBorderImages();
	}

	@Override
	public void setBounds(int x, int y, int width, int height) {
		super.setBounds(x, y, width, height);
		getPnlContainer().setBounds(0, 0, width, height);

		stretchBorderImages();
	}
	
	private void stretchBorderImages() {
		int newWidth = getPnlContainer().getWidth(); //=-348
		int newHeight = getPnlContainer().getHeight() - 46;
		ImageIcon icon;
		Image image;
		
		icon = new ImageIcon("images/border/desel_t_center.png");
		image = icon.getImage();
		icon = new ImageIcon(image.getScaledInstance(newWidth, 27, Image.SCALE_SMOOTH));
		getLblTopCenter().setIcon(icon);
		
//			icon = new ImageIcon("images/border/desel_t_bottom.png"));
//			image = icon.getImage();
//			getLblTopCenterBottom().setIcon(new ImageIcon(image.getScaledInstance(newWidth+1, 17, Image.SCALE_SMOOTH)));
		
		icon = new ImageIcon("images/border/desel_b_bottom.png");
		image = icon.getImage();
		getLblBottomCenter().setIcon(new ImageIcon(image.getScaledInstance(newWidth, 21, Image.SCALE_SMOOTH)));
		
		icon = new ImageIcon("images/border/desel_rightside.png");
		image = icon.getImage();
		getLblMiddleRight().setIcon(new ImageIcon(image.getScaledInstance(4, newHeight, Image.SCALE_SMOOTH)));
		
		icon = new ImageIcon("images/border/desel_leftside.png");
		image = icon.getImage();
		getLblMiddleLeft().setIcon(new ImageIcon(image.getScaledInstance(3, newHeight, Image.SCALE_SMOOTH)));
		
		getPnlMiddleCenter().setSize(newWidth, newHeight);
	}

	/**
	 * This method initializes pnlTop	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlTop() {
		if (pnlTop == null) {
			pnlTop = new JPanel();
			
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.insets = new Insets(0, 88, 0, 90);
			gridBagConstraints2.gridy = 2;
			gridBagConstraints2.gridx = 0;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.gridy = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.insets = new Insets(0, 90, 0, 92);
			gridBagConstraints.gridy = 0;
			gridBagConstraints.gridx = 0;
			
			GridBagLayout pnlTopLayout = new GridBagLayout();
			pnlTopLayout.columnWeights = new double[] {0.01, 1.0, 0.01};
			pnlTopLayout.columnWidths = new int[] {};
			pnlTopLayout.rowHeights = new int[] {};
			pnlTopLayout.rowWeights = new double[] {};
			
			pnlTop.setLayout(pnlTopLayout);
			pnlTop.add(getLblTopLeft(), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, -1, 0, -1), 0, 0));
			pnlTop.add(getLblTopCenter(), new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, -1, 0, 0), 0, 0));
			pnlTop.add(getLblTopRight(), new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, -1, 0, 1), 0, 0));
		}
		return pnlTop;
	}

	/**
	 * This method initializes pnlMiddle	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlMiddle() {
		if (pnlMiddle == null) {
			pnlMiddle = new JPanel();
			GridBagLayout pnlMiddleLayout = new GridBagLayout();
			pnlMiddleLayout.rowWeights = new double[] {0.1};
			pnlMiddleLayout.rowHeights = new int[] {7};
			pnlMiddleLayout.columnWeights = new double[] {0.01, 1.0, 0.01};
			pnlMiddle.setLayout(pnlMiddleLayout);
			pnlMiddle.setBackground(new java.awt.Color(255,255,255));
			pnlMiddle.add(getLblMiddleLeft(), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.VERTICAL, new Insets(0, -1, 0, 0), 0, 0));
			pnlMiddle.add(getPnlMiddleCenter(), new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
			pnlMiddle.add(getLblMiddleRight(), new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.VERTICAL, new Insets(0, 0, 0, -1), 0, 0));
		}
		return pnlMiddle;
	}

	/**
	 * This method initializes pnlBottom	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlBottom() {
		if (pnlBottom == null) {
			pnlBottom = new JPanel();
			GridBagLayout pnlBottomLayout = new GridBagLayout();
			pnlBottomLayout.rowWeights = new double[] {};
			pnlBottomLayout.rowHeights = new int[] {};
			pnlBottomLayout.columnWeights = new double[] {0.01, 1.0, 0.01};
			pnlBottomLayout.columnWidths = new int[] {};
			pnlBottom.setLayout(pnlBottomLayout);
//			pnlBottom.setPreferredSize(new java.awt.Dimension(100, 21));
//			int width = (super.getWidth() > 0) ? super.getWidth() : (int) super.getPreferredSize().getWidth();
//			int height = (super.getHeight() > 0) ? super.getHeight() : (int) super.getPreferredSize().getHeight();
//			pnlBottom.setSize(width, 21);
//			pnlBottom.setBounds(0, 0, width, 21);
			
			pnlBottom.add(getPnlBottomLeft(), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, -1, 0, 0), 0, 0));
			pnlBottom.add(getLblBottomCenter(), new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
			pnlBottom.add(getLblBottomRight(), new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 1), 0, 0));
		}
		return pnlBottom;
	}

	/**
	 * This is the default constructor
	 */
	public PanelWithBorder() {
		super();
		initialize(200, 180);
	}
	
	public PanelWithBorder(String title) {
		super();
		initialize(200, 180);
		
		this.setTitle(title);
	}
	
	public PanelWithBorder(int width, int height) {
		super();
		initialize(width, height);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize(int width, int height) {
		super.setPreferredSize(new Dimension(width, height));
		super.setLayout(null);
		
		super.setOpaque(false);
		lblTitle = new JLabel();
		lblTitle.setBounds(10, 5, width - 20, 17);
		lblTitle.setFont(new java.awt.Font("",1,11));
		lblTitle.setText("");
		super.add(lblTitle);
		super.add(getPnlContainer());
//		super.setBorder(BorderFactory.createLineBorder(Color.BLACK));
	}
	
	public void setTitle(String title) {
		lblTitle.setText(title);
	}

	private JLabel getPnlBottomLeft() {
		if(pnlBottomLeft == null) {
			pnlBottomLeft = new JLabel();
			FlowLayout pnlBottomLeftLayout = new FlowLayout();
			pnlBottomLeftLayout.setAlignment(FlowLayout.RIGHT);
			pnlBottomLeftLayout.setHgap(0);
			pnlBottomLeftLayout.setVgap(0);
			pnlBottomLeft.setLayout(pnlBottomLeftLayout);
			pnlBottomLeft.setIcon(new ImageIcon("images/border/desel_b_bottomleft.png"));
			pnlBottomLeft.setSize(9, 21);
			pnlBottomLeft.setPreferredSize(new java.awt.Dimension(9, 21));
		}
		return pnlBottomLeft;
	}
	
	private JLabel getLblBottomCenter() {
		if(lblBottomCenter == null) {
			lblBottomCenter = new JLabel();
			FlowLayout lblBottomCenterLayout = new FlowLayout();
			lblBottomCenterLayout.setHgap(0);
			lblBottomCenterLayout.setVgap(0);
			lblBottomCenter.setLayout(lblBottomCenterLayout);
			lblBottomCenter.setHorizontalAlignment(SwingConstants.CENTER);
			lblBottomCenter.setHorizontalTextPosition(SwingConstants.CENTER);
		}
		return lblBottomCenter;
	}
	
	private JLabel getLblBottomRight() {
		if(lblBottomRight == null) {
			lblBottomRight = new JLabel();
			lblBottomRight.setIcon(new ImageIcon("images/border/desel_b_bottomright.png"));
			lblBottomRight.setSize(11, 21);
			lblBottomRight.setPreferredSize(new java.awt.Dimension(11, 21));
			lblBottomRight.setHorizontalAlignment(SwingConstants.RIGHT);
		}
		return lblBottomRight;
	}
	
	private JLabel getLblMiddleRight() {
		if(lblMiddleRight == null) {
			lblMiddleRight = new JLabel();
			lblMiddleRight.setLayout(null);
			lblMiddleRight.setVerticalAlignment(SwingConstants.TOP);
			lblMiddleRight.setVerticalTextPosition(SwingConstants.TOP);
			lblMiddleRight.setOpaque(true);
		}
		return lblMiddleRight;
	}
	
	private JLabel getLblMiddleLeft() {
		if(lblMiddleLeft == null) {
			lblMiddleLeft = new JLabel();
			lblMiddleLeft.setVerticalAlignment(SwingConstants.TOP);
			lblMiddleLeft.setVerticalTextPosition(SwingConstants.TOP);
			lblMiddleLeft.setOpaque(true);
		}
		return lblMiddleLeft;
	}
	
	public JPanel getPnlMiddleCenter() {
		if(pnlMiddleCenter == null) {
			pnlMiddleCenter = new JPanel();
			pnlMiddleCenter.setOpaque(false);
//			pnlMiddleCenter.setBackground(Color.BLACK);
//			pnlMiddleCenter.setLayout(null);
		}
		return pnlMiddleCenter;
	}

	private JLabel getLblTopRight() {
		if(lblTopRight == null) {
			lblTopRight = new JLabel();
			lblTopRight.setIcon(new ImageIcon("images/border/desel_t_topright_2.png"));
			lblTopRight.setPreferredSize(new java.awt.Dimension(11, 27));
		}
		return lblTopRight;
	}
	
	private JLabel getLblTopLeft() {
		if(lblTopLeft == null) {
			lblTopLeft = new JLabel();
			lblTopLeft.setIcon(new ImageIcon("images/border/desel_t_topleft_2.png"));
			lblTopLeft.setPreferredSize(new java.awt.Dimension(9, 27));
		}
		return lblTopLeft;
	}
	
	private JLabel getLblTopCenter() {
		if(lblTopCenter == null) {
			lblTopCenter = new JLabel();
			lblTopCenter.setIcon(new ImageIcon("images/border/desel_t_center.png"));
		}
		return lblTopCenter;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame();
				frame.setLayout(null);
				
				PanelWithBorder thisClass = new PanelWithBorder(200, 180);
//				TemplateDemo thisClass2 = new TemplateDemo();
//				thisClass2.setLocation(0, 100);
				frame.add(thisClass);
				thisClass.setSize(200, 180);
				thisClass.setLocation(1, 1);
//				frame.add(thisClass2);
				
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setSize(256, 299);
				frame.setVisible(true);
			}
		});
	}
}
